<?php
/* 
 * Copyright (c) 2011 Antoine d'Otreppe de Bouvette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

class ReflectionDocumentedMethod extends ReflectionMethod {
    /**
     * @var DocComment
     */
    private $docComment;
    
    /**
     * @var ReflectionParameterInfo[]
     */
    private $parameters;
    
    /**
     * @return string
     */
    public function getReturnType() {
        $ret = $this->getDocComment()->getAnnotation('return');
        
        if ($ret !== null) {
            $parts = explode(' ', $ret->getValue(), 2);
            return $parts[0];
        }
        else {
            return null;
        }
    }
    
    public function getBrief() {
        return $this->getDocComment()->getBrief();
    }
    
    /**
     * @return DocComment
     */
    public function getDocComment() {
        if ($this->docComment === null) {
            $this->docComment = DocComment::parseDocComment(
                    parent::getDocComment());
            
            $params = array();
            foreach ($this->docComment->getAnnotationsLike('param') as $ann) {
                $parts = preg_split('/\s+/', $ann->getValue());
                
                $type = array_get($parts, 0);
                $name = ltrim(array_get($parts, 1), '$');
                $description = array_get($parts, 2);
                
                $params[$name] = array(
                    'type' => $type,
                    'description' => $description
                );
            }
            
            $this->parameters = array();
            foreach (parent::getParameters() as $param) {
                $name = $param->name;
                
                if (array_key_exists($name, $params)) {
                    $infos = $params[$name];
                }
                else {
                    $infos = array();
                }
                
                if ($param->isArray()) {
                    // If it's defined as an array, no matter the doc,
                    // it's an array
                    $infos['type'] = 'array';
                }
                elseif ($param->getClass() !== null) {
                    // Same as above
                    $infos['type'] = $param->getClass()->getName();
                }

                try {
                    $default = $param->getDefaultValue();
                    $optional = true;
                }
                catch (ReflectionException $e) {
                    $optional = false;
                    $default = null;
                }

                $this->parameters[$name] = new ReflectionParameterInfo(
                        $name, array_get($infos, 'type'),
                        array_get($infos, 'description'), $optional, $default);
            }
        }
        
        return $this->docComment;
    }
    
    /**
     * @return ReflectionParameterInfo[]
     */
    public function getParameterInfos() {
        return $this->parameters;
    }
}
